import { login, getInfo, staffPhoto } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
// 暴露子仓库的实例对象
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    updateToken(state, token) {
      state.token = token
      // localStorage.setItem('token', token)
      setToken(token)
    },
    delToken(state) {
      state.token = ''
      removeToken()
    },
    userInfo(state, info) {
      state.userInfo = info
    }

  },
  actions: {
    async  userLogin(context, loginForm) {
      const res = await login(loginForm)
      // console.log(res)
      context.commit('updateToken', res.data)
    },
    async updateUserInfo(context) {
      const { data: info } = await getInfo()
      const { data: photo } = await staffPhoto(info.userId)
      // console.log(photo)
      context.commit('userInfo', { ...info, ...photo })
      // console.log(info, '------------')
      return info.roles.menus
    },
    removeUserInfo(context) {
      context.commit('delToken')
      context.commit('userInfo', '')
      resetRouter()
    }
  }
}
